$(function () {
    //定义常用easyui对象
    var gift_datagrid = $("#gift_datagrid");
    var gift_dialog = $("#gift_dialog");
    //end

    //初始化礼品列表 gift_datagrid
    gift_datagrid.datagrid({
        title:"礼品列表",
        url:'/salary/list',
        fitColumns:true,
        fit:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        striped:true,
        toolbar: '#tb',
        columns:[[
            {field:'id',title:'编号',width:100},
            {field:'name',title:'姓名',width:100},
            {field:'position',title:'职位',width:100,align:'right'},
            {field:'basePay',title:'基本工资',width:100,align:'right'},
            {field:'performancePay',title:'绩效工资',width:100,align:'right'},
            {field:'technologyPay',title:'技术补贴',width:100,align:'right'},
            {field:'phonePay',title:'通讯费',width:100,align:'right'},
            {field:'awardPay',title:'奖励',width:100,align:'right'},
            {field:'overworkPay',title:'加班',width:100,align:'right'},
            {field:'stayworkPay',title:'值班',width:100,align:'right'},
            {field:'teachCost',title:'讲课费',width:100,align:'right'},
            {field:'yearPay',title:'年终奖',width:100,align:'right'},
            {field:'olderPay',title:'工资工龄',width:100,align:'right'},
            {field:'complement',title:'补足',width:100,align:'right'},
            {field:'cost',title:'扣款',width:100,align:'right'},
            {field:'workdateCost',title:'考勤扣款',width:100,align:'right'},
            {field:'salary',title:'应发工资',width:100,align:'right'},
            {field:'socialCost',title:'社保',width:100,align:'right'},
            {field:'funds',title:'公积金',width:100,align:'right'},
            {field:'taxCost',title:'个所税',width:100,align:'right'},
            {field:'trueSalary',title:'实发工资',width:100,align:'right'},
            {field:'remark',title:'备注',width:100,align:'right'}
        ]],
        onClickRow:function (index,row) {
            alert(111)
            alert(row.id);
            var id = row.id;
            $("#empId").val(id);
        }
    });
    //end


    //初始化保存或编辑对话框
    gift_dialog.dialog({
        width: 300,
        height: 500,
        closed: true,
        buttons:"#bb"
    });



    //响应集合函数对象
    var cmdObj = {
        //顶部工具栏的刷新
        reload: function () {
            alert("刷新啦~")
            gift_datagrid.datagrid("load");
        },
        //顶部工具栏的删除
        remove: function () {
            //获取要删除的行
            var row = gift_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示','请选择要删除的行');
                return;
            }
            $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
                if (r){
                    //删除ajax请求
                    $.get("/gift/delete?",{id:row.id},function (data) {
                        if(!data.success){
                            $.messager.alert('温馨提示',data.msg);
                        }else{
                            $.messager.alert('温馨提示',"删除成功");
                            gift_datagrid.datagrid("load");
                        }
                    })
                }
            });

        },
        //顶部工具栏的添加
        add: function () {
            //添加与编辑时将数据清空
            $("#gift_form").form("clear");
            gift_dialog.dialog("open");
            gift_dialog.dialog("setTitle","新增礼品");



        },
        //顶部工具栏的编辑
        edit: function () {
            //判断是否选中数据
            var row = gift_datagrid.datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示','请选择要编辑的行');
            }else{
                //添加与编辑时将对话框中数据清空
                $('#gift_form').form("clear");
                //数据回显: 同名原则
                $("#gift_form").form("load", row);
                gift_dialog.dialog("open");
                gift_dialog.dialog("setTitle","编辑礼品");
            }

        },
        //顶部工具栏的删除
        save:function () {
            var id = $("#giftId").val();
            alert(id);
            var url =  '/gift/save';

            if(id){
                url =  '/gift/update';
            }
            $("#gift_form").form('submit', {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);

                    if(!data.success){
                        $.messager.alert('温馨提示',data.msg);

                    }else{
                        //关闭框
                        gift_dialog.dialog("close");
                        //刷新列表数据
                        gift_datagrid.datagrid("load");
                    }
                }
            })
        },

        //高级查询
        query:function () {
            //获取高级查询输入框的文本
            var kw = $("#keyword").textbox("getValue")
            if(!kw){
                $.messager.alert('温馨提示','请输入关键字');
                return;
            }
            gift_datagrid.datagrid("load",{
                keyword:kw
            })
        }
        //end

    };
    //end


    //调用
    $("a[data-cmd]").click(function(){
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })
    //end
})